package org.rsbot.script.randoms.antiban;

import java.util.LinkedList;
import java.util.List;

import org.rsbot.script.Antiban;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.wrappers.RSInterface;
import org.rsbot.script.wrappers.RSInterfaceChild;

/*
 * Cleaned and Updated By Secret Spy for Infinity
 * Updated On 04/16/11
 */
@ScriptManifest(authors = {"Pervy Shuya", "Secret Spy"}, name = "Interface Closer", version = 1.3)
public class InterfaceCloser extends Antiban {

    private final List<RSInterfaceChild> components = new LinkedList<RSInterfaceChild>();
    
    public InterfaceCloser() {
        addChild(743, 20); /* Audio */
        addChild(767, 10); /* Bank help */
        addChild(499, 29); /* Stats */
        addChild(594, 48); /* Report */
        addChild(275, 8); /* Quest/Achievement Diary */
        addChild(266, 11); /* Grave */
        addChild(206, 16); /* Price-checker */
        addChild(102, 13); /* Items Kept On Death */
        addChild(14, 3); /* Pin settings */
        addChild(157, 13); /* Quick chat help */
        addChild(764, 2); /* Objectives */
        addChild(895, 19); /* Advisor */
    }

    private void addChild(final int parent, final int idx) {
        System.out.println(ifaces + " yu");
        components.add(ifaces.getInterface(parent).getChild(idx));
    }

    @Override
    public boolean activateCondition() {
        if (isLoggedIn()) {
            if (ifaces.getInterface(755).getChild(44).isValid()) { // World map
				if (ifaces.getChildInterface(755, 0).getComponents().length > 0)
                    return true;
			}
            for (final RSInterfaceChild c : components) {
				if (c.isValid() &&
                	c.getAbsoluteX()!=-1 && c.getAbsoluteY()!=-1)
                    return true;
			}
		}
		return false;
	}

    @Override
    public int loop() {
        wait(random(500, 900));

		if (ifaces.getInterface(755).isValid() && (ifaces.getChildInterface(755, 0).getComponents().length > 0)) {
			atInterface(755, 44);
			return random(500, 900);
		}
        for (final RSInterfaceChild c : components) {
            if (c.isValid()) {
                atInterface(c);
                wait(random(500, 900));
                break;
            }
        }
        return -1;
    }
}